const { MongoClient } = require("mongodb");

const client = new MongoClient("mongodb://localhost:27017", {
  useUnifiedTopology: true,
});

// 开启连接
async function run() {
  try {
    // 开始连接
    await client.connect();
    const db = client.db("hello");
    const inventoryCollection = db.collection("inventory");
    const ret = await inventoryCollection.find();
    // ret是一个游标对象，需要调用toArray方法获取数据
    console.log(await ret.toArray());
  } catch (err) {
    console.log(err);
  } finally {
    // 关闭连接
    await client.close();
  }
}

run();
